package com.lxg.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 陆小根
 * date: 2022/04/24
 * Description:
 */

@RestController
public class TestController {

  @RequestMapping("/user")
  public String user() {
    // 1.代码中获取用户信息
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    // 2.通过获取用户信息
    User user = (User) authentication.getPrincipal(); // 用的是内存中的数据
    System.out.println(user.getUsername());
    System.out.println(user.getAuthorities());
    return "user ok";
  }

}
